package com.yfp.operation.controller;

import com.yfp.common.core.domain.R;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.operation.domain.OrderListDO;
import com.yfp.operation.domain.UserBill;
import com.yfp.operation.enums.UserBillStatusEnums;
import com.yfp.operation.service.BillProfitSharingService;
import com.yfp.operation.service.OrderListService;
import com.yfp.operation.service.UserBillService;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户分成账单
 */
@Slf4j
@RestController
@RequestMapping("/userBill")
@Tag(name = "用户分成账单")
public class UserBillController extends BaseController {

    @Resource
    private UserBillService userBillService;
    @Resource
    private BillProfitSharingService billProfitSharingService;
    @Resource
    private OrderListService orderListService;

    @Autowired
    private ApplicationContext applicationContext;

    @Operation(summary = "订单分成", description = "pay服务使用")
    @GetMapping("/order/routing")
    public R<Void> orderRouting(@ApiParam(value = "订单编号") String orderSn, @ApiParam(value = "支付类型") Integer payType) {
        log.info("用户分成账单：订单分成,订单编号:{}， 支付类型：{}", orderSn, payType);
        userBillService.orderRouting(orderSn, payType);
        return R.ok();
    }

    @Operation(summary = "订单分成是否提现", description = "退款服务使用")
    @GetMapping("/order/withdraw")
    public R<Boolean> orderRouting(@ApiParam(value = "订单编号") String orderSn) {
        log.info("订单分成是否提现：订单编号:{}", orderSn);
        List<UserBill> userBills = userBillService.getByOrderSnAndStatus(orderSn, UserBillStatusEnums.WITHDRAWN.getStatus());
        return R.ok(CollectionUtils.isEmpty(userBills));
    }

    @Operation(summary = "订单退款扣除之前的分成", description = "pay服务使用")
    @GetMapping("/order/reimburse")
    public R<Void> orderReimburse(@ApiParam(value = "订单编号") String orderSn, @ApiParam(value = "退款金额") BigDecimal refundMoney) {
        log.info("用户分成账单：订单退款扣除之前的分成,订单编号:{}, 退款金额:{}", orderSn, refundMoney);
        userBillService.orderReimburse(orderSn, refundMoney);
        return R.ok();
    }

    @Operation(summary = "订单入账", description = "定时任务每天凌晨2点执行")
    @GetMapping("/orderBill/carryto")
    public R<Void> orderBillCarryto() {
        userBillService.orderBillCarryto();
        return R.ok();
    }

    @Operation(summary = "自动打款分账", description = "定时任务每天22点执行")
    @GetMapping("/orderBill/autoMakePayment")
    public R<Void> autoMakePayment() {
        userBillService.autoMakePayment();
        return R.ok();
    }

    @Operation(summary = "取消冻结", description = "取消冻结")
    @GetMapping("/unfreeze")
    public R<Void> unfreeze(String payTradeNo) {
        List<OrderListDO> finishs = orderListService.findFinish();
        for (OrderListDO finish : finishs) {
            billProfitSharingService.unfreeze(finish.getPayTradeNo());
            try {
                Thread.sleep(100);
            } catch (Exception e){
                e.printStackTrace();
            }
        }
        return R.ok();
    }


    @Operation(summary = "企业零钱到账", description = "企业零钱到账")
    @GetMapping("/testEndPay")
    public R<Void> testEndPay() {
        try {
            billProfitSharingService.testEntPay();
        } catch (Exception e){
            e.printStackTrace();
        }
        return R.ok();
    }
}